home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / WackyWindows / Libs / animate.c next >
Encoding:
Text File  |  1998-06-13  |  3.5 KB  |  125 lines  |  [TEXT/CWIE]

  1. // ******************************************************************
  2. //    program written by 
  3. //                         Paul Baxter
  4. //                         MacHack'98
  5. //
  6. //
  7. // ******************************************************************
  8. #include <StdLib.h>
  9.  
  10. #include "patch.h"
  11. #include "animate.h"
  12.  
  13. // * ******************************************************************************
  14. // *     AnimateWindow
  15. // *             Bounce the window around the screen
  16. // * ******************************************************************************
  17. void AnimateWindow(WindowPtr theWindow, Point* savePt)
  18. {
  19.     GrafPtr oldPort;
  20.     Rect winRect, grayRect;
  21.     short dx, dy, bounce, maxbounces, step;
  22.  
  23.     GetPort(&oldPort);
  24.     SetPort(theWindow);
  25.  
  26.     savePt->h = savePt->v = 0;
  27.     LocalToGlobal(savePt);
  28.  
  29.     winRect = ((WindowPeek)theWindow)->port.portRect;    
  30.     LocalToGlobal(&topLeft(winRect));
  31.     LocalToGlobal(&botRight(winRect));
  32.  
  33.     SetPort(oldPort);
  34.  
  35.     if (((WindowPeek)theWindow)->visible) {
  36.  
  37.         grayRect = (*LMGetGrayRgn())->rgnBBox;
  38.  
  39.         if (RectInRect(&winRect, &grayRect)) {
  40.             dx = myRandom(kMinSpeed, kStartSpeed);
  41.             dy = myRandom(kMinSpeed, kStartSpeed);
  42.             if (myRandom(kDirectionChangeLow, kDirectionChangeHi) > kDirectionThreshHold)
  43.                 dx = -dx;
  44.             if (myRandom(kDirectionChangeLow, kDirectionChangeHi) > kDirectionThreshHold)
  45.                 dy =-dy;
  46.  
  47.             maxbounces = myRandom(kMinBounces, kMaxBounces);
  48.             for (bounce = 0; bounce < maxbounces; bounce++) {
  49.                 do {
  50.                     MoveWindow(theWindow, winRect.left, winRect.top, false);        
  51.                     OffsetRect(&winRect, dx, dy);
  52.                 } while (RectCompletelyInRect(&winRect, &grayRect, &dx, &dy));
  53.                 if (gSoundPlayer)
  54.                     CALL_SOUNDPROC(gSoundPlayer);
  55.                 OffsetRect(&winRect, dx, dy);
  56.             }
  57.             MoveWindow(theWindow, winRect.left, winRect.top, false);        
  58.             dx = (savePt->h - winRect.left) / kStepsBack;
  59.             dy = (savePt->v - winRect.top) / kStepsBack;
  60.             for (step = 0; step < kStepsBack; step++) {
  61.                 OffsetRect(&winRect, dx, dy);
  62.                 MoveWindow(theWindow, winRect.left, winRect.top, false);        
  63.             }
  64.         }
  65.         MoveWindow(theWindow, savePt->h, savePt->v, false);
  66.     }
  67. }
  68.  
  69. // * ******************************************************************************
  70. // *     myRandom
  71. // *             generate random numbers with a min and max
  72. // * ******************************************************************************
  73. short myRandom(short min, short max )
  74. {
  75.     unsigned    short rnd;
  76.     long        range, t;
  77.  
  78.     rnd = (unsigned    short)rand();
  79.     range = max - min;
  80.     t = (rnd * range) / RAND_MAX;
  81.     return (short)( t+min );
  82. }
  83.  
  84. // * ******************************************************************************
  85. // *     RectInRect
  86. // *             Determine if 2 Rects intersect
  87. // * ******************************************************************************
  88. Boolean RectInRect(Rect* rect1, Rect* rect2)
  89. {
  90.     return !(
  91.         (rect1->bottom < rect2->top) ||
  92.         (rect1->top > rect2->bottom) ||
  93.         (rect1->right < rect2->left) ||
  94.         (rect1->left > rect2->right)
  95.     );
  96. }
  97.  
  98. // * ******************************************************************************
  99. // *     RectInRect
  100. // *             Determine if rect1 is enclosed in rect2
  101. // *                If not change direction and speed
  102. // * ******************************************************************************
  103. Boolean RectCompletelyInRect(Rect* rect1, Rect* rect2, short* dx, short* dy)
  104. {
  105.     Boolean result;
  106.     
  107.     result = true;
  108.     if ((rect1->bottom > rect2->bottom) ||
  109.         (rect1->top< rect2->top)) {
  110.  
  111.         *dy = -(*dy);
  112.         (*dy) *= myRandom(kMinSpeedMultiPlier, kMaxSpeedMultiplier);
  113.         result = false;
  114.  
  115.     }
  116.     if ((rect1->right > rect2->right) ||
  117.         (rect1->left< rect2->left)) {
  118.  
  119.         *dx = -(*dx);
  120.         *(dx) *= myRandom(kMinSpeedMultiPlier, kMaxSpeedMultiplier);
  121.         result = false;
  122.     }
  123.     return result;
  124. }
  125.